<?php

namespace app\admin\controller\wadviser;

use app\common\controller\Backend;
use app\common\model\AdviserConfig as ConfigModel;
use think\Exception;
/**
 * 配置
 *
 * @icon fa fa-user-secret
 */
class Config extends Backend
{
    protected $model = null;
    protected $searchFields = ['title'];


    public function _initialize()
    {
        parent::_initialize();
        $this->assignconfig("adviser_id", input('adviser_id'));
        $this->model = new ConfigModel;
        $this->view->assign('typeList', ConfigModel::getTypeList());
        $this->assignconfig("typeList", ConfigModel::getTypeList());
    }
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model

                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array(
                "total" => $list->total(),
                "rows" => $list->items()
            );
            return json($result);
        }
        return $this->view->fetch();
    }
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a',[], 'trim');
            if ($params) {
                $params['adviser_id'] = input('adviser_id');
                if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
                    $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
                } else {
                    $params['content'] = '';
                }
                try {
                    $result = $this->model->create($params);
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    
                    $this->success();
                } else {
                    $this->error($this->model->getError());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }
        
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            if ($params) {
                if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio'])) {
                    $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
                } else {
                    $params['content'] = '';
                }
                $result = $row->save($params);
                if ($result !== false) {
                    $this->success();
                }
                $this->error($row->getError());
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        if (in_array($row['type'], ['select', 'selects', 'checkbox', 'radio'])) {
            $value['value'] = explode(',', $row['value']);
        }
        
        $row['content'] = ConfigModel::encode(json_decode($row['content'],true));
        $row['tip'] = htmlspecialchars($row['tip']);
        
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }
}